package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.TbUser;
import com.qf.xiaomi.service.impl.userServiceImpl;
import com.qf.xiaomi.service.userService;
import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.utils.StringUtils;
import com.qf.xiaomi.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    userService service=new userServiceImpl();
    public String register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String gender = req.getParameter("gender");
        gender=new String(gender.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        System.out.println(gender);
        if(StringUtils.isEmpty(username)&&!StringUtils.isLenghtEnough(username,3,10)){
//            resp.sendRedirect("/error/error.jsp");
            req.setAttribute("exp","用户名不能为空，且长度为3--10");
            return StringConstans.FORWARD+"/error/error.jsp";
        }
        if(StringUtils.isEmpty(password)&&!StringUtils.isLenghtEnough(password,3,6)){
//            resp.sendRedirect("/error/error.jsp");
            req.setAttribute("exp","密码不能为空且密码长度为5--10");
            return "forward:/error/error.jsp";
        }
        if (!email.matches(StringConstans.EMAILREGE)){
//            resp.sendRedirect("/error/error.jsp");
            req.setAttribute("exp","邮箱地址不符合规范");
            return  StringConstans.FORWARD+"/error/error.jsp";
        }
        System.out.println("数据校验没问题");
        TbUser user = new TbUser();
        user.setEmail(email);
        user.setGender(gender);
        user.setPassword(password);
        user.setUsername(username);
        int count = service.addUser(user);
        if (count==1){
            return StringConstans.REDIRECT+"registerSuccess.jsp";
        }else {
            req.setAttribute("exp","注册失败");
            return StringConstans.FORWARD+"/error/error.jsp";
        }
    }

    public String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        if (StringUtils.isEmpty(code)){
            req.setAttribute("exp","验证码不能为空");
            return StringConstans.FORWARD+"/error/error.jsp";
        }
        HttpSession session = req.getSession();
        String codeSave = (String) session.getAttribute("code");
        if (!code.equalsIgnoreCase(codeSave)){
            req.setAttribute("exp","验证码输入错误");
            return StringConstans.FORWARD + "/error/error.jsp";
        }
        //验证码校验成功；
        TbUser user = service.login(username, password);
        if (user!=null){
            session.setAttribute("user",user);
            CompletableFuture.runAsync(()->{
                service.updateLoginTime(user.getId());
            });
            return StringConstans.REDIRECT+"/index.jsp";
        }else {
            req.setAttribute("exp","登陆失败");
            return StringConstans.FORWARD+"/error/error.jsp";
        }

    }

//    public String addAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String detail = req.getParameter("detail");
//        String name = req.getParameter("name");
//        String phone = req.getParameter("phone");
//        String uidStr = req.getParameter("uid");
//        detail=new String(detail.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//        name=new String(name.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//        System.out.println("name="+name);
//        int uid = Integer.parseInt(uidStr);
//        if (StringUtils.isEmpty(detail)){
//            req.setAttribute("exp","地址不能为空");
//            return  StringConstans.FORWARD+"/error/error.jsp";
//        }
//        if (StringUtils.isEmpty(name)){
//            req.setAttribute("exp","姓名不能为空");
//            return  StringConstans.FORWARD+"/error/error.jsp";
//        }
//        if (!phone.matches(StringConstans.PHONEREGE)){
//            req.setAttribute("exp","手机号格式不正确");
//            return  StringConstans.FORWARD+"/error/error.jsp";
//        }
//        if (!StringUtils.isexist(uid)){
//            req.setAttribute("exp","uid不存在请输入正确的uid");
//            return  StringConstans.FORWARD+"/error/error.jsp";
//        }
//        System.out.println("校验成功，可以添加用户地址");
//        Address address = new Address();
//        address.setDetail(detail);
//        address.setName(name);
//        address.setPhone(phone);
//        address.setUid(uid);
//        service.addAddress(address);
//        return  null;
//    }
}
